package beans;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import beansInterfaces.BookingMgr;

import javax.annotation.Resource;
import javax.annotation.security.RolesAllowed;
import javax.ejb.EJBContext;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import dto.BookingDTO;

import entities.BookingEntity;
import entities.GroupEnum;
import entities.PersonalizedBookingEntity;

/**
 * Session Bean implementation class BookingMgrBean
 */
@Stateless
public class BookingMgrBean implements BookingMgr {

	@PersistenceContext
    private EntityManager em;
	
	private PackageMgrBean pmb;
	
	@Resource
	private EJBContext context;
	
    /**
     * Default constructor. 
     */
    public BookingMgrBean() {
        // TODO Auto-generated constructor stub
    }

    //Book 
    @Override
	@RolesAllowed({GroupEnum._USER})
	public void bookPackage(BookingDTO booking) {

		em.persist(new BookingEntity(booking));
	}
	
    @Override
	@RolesAllowed({GroupEnum._USER})
	public void bookPersonalizedPackage(BookingDTO booking) {

		em.persist(new PersonalizedBookingEntity(booking));
	}
	
	
    //Query
	public List<BookingEntity> getUserBookings(String user){
		return em.createNamedQuery(BookingEntity.FIND_MY_BOOKINGS, BookingEntity.class)
				.setParameter("user", user).getResultList();
	}
	
	public List<PersonalizedBookingEntity> getPersonalizedUserBookings(String user){
		return em.createNamedQuery(PersonalizedBookingEntity.FIND_MY_BOOKINGS, PersonalizedBookingEntity.class)
				.setParameter("user", user).getResultList();
	}
	

	public List<BookingEntity> findInvitation(String username, int idB){
		return em.createNamedQuery(BookingEntity.FIND_INVITATION, BookingEntity.class).setParameter("id", idB).setParameter("username", username).getResultList();
	}
	
	public List<PersonalizedBookingEntity> findPersonalizedInvitation(String username, int idB){
		return em.createNamedQuery(PersonalizedBookingEntity.FIND_INVITATION, PersonalizedBookingEntity.class).setParameter("id", idB).setParameter("username", username).getResultList();
	}
	
	//Create List Of DTO of Invitations
	
    @Override

	public List<BookingDTO> getInvitation(String username, int id){
		Iterator<BookingEntity> iterator1 = findInvitation(username, id).iterator();
		Iterator<PersonalizedBookingEntity> iterator2 = findPersonalizedInvitation(username, id).iterator();

    	List<BookingDTO> DTOList = new ArrayList<BookingDTO>();
    	
    	while(iterator1.hasNext())
		  {
			  DTOList.add(convertToDTO(iterator1.next()));
		  }
		
    	while(iterator2.hasNext())
		  {
			  DTOList.add(convertToDTO(iterator2.next()));
		  }
		return DTOList;
    	
	}
	
	

	
	//Create List Of DTO of Bookings
    @Override
	
	public List<BookingDTO> getUserBookingsDTO(String user){
		Iterator<BookingEntity> iterator1 = getUserBookings(user).iterator();
		Iterator<PersonalizedBookingEntity> iterator2 = getPersonalizedUserBookings(user).iterator();

    	List<BookingDTO> DTOList = new ArrayList<BookingDTO>();
    	
    	while(iterator1.hasNext())
		  {
			  DTOList.add(convertToDTO(iterator1.next()));
		  }
    	
    	while(iterator2.hasNext())
		  {
			  DTOList.add(convertToDTO(iterator2.next()));
		  }
    	
    	
		return DTOList;
		
	}
		

	
	
	//Convert To DTO
	
	public BookingDTO convertToDTO(BookingEntity booking){
		BookingDTO bookingDTO = new BookingDTO();
		this.pmb=new PackageMgrBean();
		bookingDTO.setPackageChosen(pmb.convertToDTO(booking.getPackageChosen()));
		bookingDTO.setUser(booking.getUser());
		return bookingDTO;
	}
	
	public BookingDTO convertToDTO(PersonalizedBookingEntity booking){
		this.pmb=new PackageMgrBean();
		BookingDTO bookingDTO = new BookingDTO();
		bookingDTO.setPackageChosen(pmb.convertToDTO(booking.getPackageChosen()));
		bookingDTO.setUser(booking.getUser());
		return bookingDTO;
	}

}
